<?php
declare (strict_types=1);

namespace app\listener\agent;

use app\event\OldAgent;
use app\model\Agent;
use app\model\PageSetting;
use app\model\TakeoutSlider;
use app\model\TakeoutVajra;

class Takeout
{
    /**
     * 事件监听处理
     * @param OldAgent $event
     * @return mixed
     */
    public function handle(OldAgent $event)
    {
        /**
         * @var Agent
         */
        $agent = $event->agent;
        $pageSetting = new PageSetting();

        //1.查询系统之前的本地生活首页配置数据
        $slider = TakeoutSlider::query()->where('agent_id', $agent->id)
            ->where('type', TakeoutSlider::TYPE_TACKOUT)
            ->select();

        //1.查询系统之前的本地生活金刚区配置数据
        $vajra = TakeoutVajra::query()->where('agent_id', $agent->id)
            ->where('type', TakeoutSlider::TYPE_TACKOUT)
            ->select();

        //都没有数据的话return
        if ($slider->isEmpty() && $vajra->isEmpty()) {
            return;
        }
        $params = [
            'type'          =>  PageSetting::TYPE_TACKOUT,
            'agent_id'      =>  $agent->id,
            'source'        =>  PageSetting::SOURCE_ZDY,
        ];
        //轮播图
        if (!$slider->isEmpty()) {
            $params['sub_category'] = PageSetting::SUB_CATEGORY_SYLBT;
            $slider->each(function (TakeoutSlider $takeoutSlider) use ($pageSetting, $params) {
                $ext_data = $takeoutSlider->params;
                if ($ext_data) {
                    $ext_data_params = json_decode($ext_data,true);
                }
                $params['setting_name'] =   $takeoutSlider->name;
                $params['image']        =   $takeoutSlider->image??'';
                $params['jump_type']    =   $ext_data_params['type']??'';
                $params['path']         =   $ext_data_params['path']??'';
                $params['sort']         =   $takeoutSlider->sort??0;
                $pageSetting->doSave($params);
                unset($params['setting_name']);
                unset($params['image']);
                unset($params['jump_type']);
                unset($params['path']);
            });
        }

        //金刚区
        if (!$vajra->isEmpty()) {
            $params['sub_category'] = PageSetting::SUB_CATEGORY_SYJGQ;
            $vajra->each(function (TakeoutVajra $takeoutVajra) use ($pageSetting, $params) {
                $ext_data = $takeoutVajra->params;
                if ($ext_data) {
                    $ext_data_params = json_decode($ext_data,true);
                }
                $params['setting_name'] =   $takeoutVajra->name;
                $params['image']        =   $takeoutVajra->image??'';
                $params['jump_type']    =   $ext_data_params['type']??'';
                $params['path']         =   $ext_data_params['path']??'';
                $params['sort']         =   $takeoutVajra->sort??0;
                $pageSetting->doSave($params);
                unset($params['setting_name']);
                unset($params['image']);
                unset($params['jump_type']);
                unset($params['path']);
            });
        }

    }
}
